package ch.zbw.pra.sysventory.dataaccess.dataAccessObject;

import java.io.Serializable;
import java.util.List;

import org.springframework.context.ApplicationContext;

import ch.zbw.pra.sysventory.database.jdbcTemplate.ComputerJDBCTemplate;
import ch.zbw.pra.sysventory.model.Computer;
import ch.zbw.pra.sysventory.model.HardDisk;
import ch.zbw.pra.sysventory.model.Nic;
import ch.zbw.pra.sysventory.model.OperatingSystem;
import ch.zbw.pra.sysventory.model.Printer;
import ch.zbw.pra.sysventory.model.Processor;
import ch.zbw.pra.sysventory.model.Software;
import ch.zbw.pra.sysventory.utils.DatabaseUtils;

/**
 * Bereitet Daten für den Zugriff auf die Datenbank im DatabaseLayer auf
 * 
 * @author Ralph Koster & Roberto Lucas
 */

public class ComputerDataAccessObject implements Serializable {

	private static final long serialVersionUID = -3448440756946993785L;
	
	private ApplicationContext context;
	private ComputerJDBCTemplate computerJDBCTemplate;
	private OperatingSystemDataAccessObject operatingSystemDataAccessObject;
	private ProcessorDataAccessObject processorDataAccessObject;
	private HardDiskDataAccessObject hardDiskDataAccessObject;
	private PrinterDataAccessObject printerDataAccessObject;
	private NicDataAccessObject nicDataAccessObject;
	private SoftwareDataAccessObject softwareDataAccessObject;
	
	public ComputerDataAccessObject() {
		context = DatabaseUtils.getContext();
		computerJDBCTemplate = (ComputerJDBCTemplate)context.getBean("computer");
		
		this.operatingSystemDataAccessObject = new OperatingSystemDataAccessObject();
		this.processorDataAccessObject = new ProcessorDataAccessObject();
		this.hardDiskDataAccessObject = new HardDiskDataAccessObject();
		this.printerDataAccessObject = new PrinterDataAccessObject();
		this.nicDataAccessObject = new NicDataAccessObject();
		this.softwareDataAccessObject = new SoftwareDataAccessObject();
	}

	public List<Computer> getAllComputerFromDatabase() {
		List<Computer> computers = computerJDBCTemplate.listComputers();
		for(Computer computer : computers) {
			Integer computerId = computer.getId();
			computer.setOs(getOsFromComputer(computerId));
			computer.setProcessor(getProcessorFromComputer(computerId));
			computer.setHardDisks(getHardDisksFromComputer(computerId));
			computer.setPrinters(getPrintersFromComputer(computerId));
			computer.setNics(getNicsFromComputer(computerId));
			computer.setSoftwares(getSoftwaresFromComputer(computerId));
		}
		return computers;
	}
	
	public List<Computer> getComputersFromScanversion(Integer scanversionId) {
		List<Computer> computers = getAllComputerFromScanversion(scanversionId);
		for(Computer computer : computers) {
			Integer computerId = computer.getId();
			computer.setOs(getOsFromComputer(computerId));
			computer.setProcessor(getProcessorFromComputer(computerId));
			computer.setHardDisks(getHardDisksFromComputer(computerId));
			computer.setPrinters(getPrintersFromComputer(computerId));
			computer.setNics(getNicsFromComputer(computerId));
			computer.setSoftwares(getSoftwaresFromComputer(computerId));
		}
		return computers;
	}
	
	private OperatingSystem getOsFromComputer(Integer computerId) {
		OperatingSystem operatingSystem = operatingSystemDataAccessObject.getOperatingSystemFromComputer(computerId);
		return operatingSystem;
	}
	
	private Processor getProcessorFromComputer(Integer computerId) {
		Processor processor = processorDataAccessObject.getProcessorFromComputer(computerId);
		return processor;
	}
	
	private List<HardDisk> getHardDisksFromComputer(Integer computerId) {
		List<HardDisk> hardDisks = hardDiskDataAccessObject.getAllHardDisksFromComputer(computerId);
		return hardDisks;
	}
	
	private List<Printer> getPrintersFromComputer(Integer computerId) {
		List<Printer> printers = printerDataAccessObject.getAllPrinterFromComputer(computerId);
		return printers;
	}

	private List<Nic> getNicsFromComputer(Integer computerId) {
		List<Nic> nics = nicDataAccessObject.getAllNicsFromComputer(computerId);
		return nics;
	}

	private List<Software> getSoftwaresFromComputer(Integer computerId) {
		List<Software> softwares = softwareDataAccessObject.getAllSoftwaresFromComputer(computerId);
		return softwares;
	}

	public List<Computer> getAllComputerFromScanversion(Integer scanversionId) {
		List<Computer> computers = computerJDBCTemplate.listComputersFromScanversion(scanversionId);
		return computers;
	}

	public Integer writeComputerToDatabase(Computer computer, Integer scanversionId) {
		Integer computerId = computerJDBCTemplate.insertComputer(computer, scanversionId);
		return computerId;
	}
	
	public void saveComputer(Computer computer, Integer scanversionId) {
		Integer computerId = writeComputerToDatabase(computer, scanversionId);
		saveOperatingSystem(computer, computerId);
		saveProcessor(computer, computerId);
		saveHardDisks(computer, computerId);
		saveSoftwares(computer, computerId);
		saveNics(computer,computerId);
		savePrinters(computer,computerId);
	}
	
	private void saveOperatingSystem(Computer computer, Integer computerId) {
		OperatingSystem operatingSystem = computer.getOs();
		operatingSystemDataAccessObject.writeOperatingSystemToDatabase(operatingSystem, computerId);
	}
	
	private void saveProcessor(Computer computer, Integer computerId) {
		Processor processor = computer.getProcessor();
		processorDataAccessObject.writeProcessorToDatabase(processor, computerId);
	}
	
	private void saveHardDisks(Computer computer, Integer computerId) {
		List<HardDisk> hardDisks = computer.getHardDisks();
		hardDiskDataAccessObject.writeHardDisksToDatabase(hardDisks, computerId);
	}
	
	private void saveSoftwares(Computer computer, Integer computerId){
		List<Software> softwares = computer.getSoftwares();
		softwareDataAccessObject.writeSoftwaresToDatabase(softwares,computerId);
	}
	
	private void saveNics(Computer computer, Integer computerId) {
		List<Nic> nics = computer.getNics();
		nicDataAccessObject.writeNicsToDatabase(nics,computerId);
	}
	
	private void savePrinters(Computer computer, Integer computerId) {
		List<Printer> printers = computer.getPrinters();
		printerDataAccessObject.writePrintersToDatabase(printers, computerId);
	}
}
